package com.baomidou.mybatisplus.samples.quickstart.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.samples.quickstart.aop.annotation.Loggable;
import com.baomidou.mybatisplus.samples.quickstart.entity.SysUser;
import com.baomidou.mybatisplus.samples.quickstart.entity.condition.SysUserCondition;
import com.baomidou.mybatisplus.samples.quickstart.mapper.SysUserMapper;
import com.baomidou.mybatisplus.samples.quickstart.service.interfaces.SysUserServiceInterface;
import com.example.entity.MicroPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 03:26
 **/

@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> implements SysUserServiceInterface {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Loggable
    public List<SysUser> findAll() {
        return sysUserMapper.selectList(null);
    }

    @Loggable
    public Optional<SysUser> findById(Long id) {
        return Optional.ofNullable(sysUserMapper.selectById(id));
    }

    @Loggable
    public SysUser isave(SysUser user) {
        sysUserMapper.insert(user);
        return user;
    }

    @Loggable
    public Optional<SysUser> update(SysUser user) {
        int i = sysUserMapper.updateById(user);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }

    @Loggable
    public boolean delete(Long id) {
        if (sysUserMapper.selectById(id) != null) {
            sysUserMapper.deleteById(id);
            return true;
        }
        return false;
    }

    @Loggable
    public MicroPage<SysUser> page(int current, int size, SysUserCondition condition) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        if(condition != null && condition.getName() != null){
            wrapper.like("name", condition.getName()).or();
        }
        if(condition != null && condition.getAge() != null){
            wrapper.like("age", condition.getAge()).or();
        }
        if(condition != null && condition.getEmail() != null){
            wrapper.like("email", condition.getEmail()).or();
        }
        Page<SysUser> page = new Page<>(current, size);
        Page<SysUser> sysUserPage = sysUserMapper.selectPage(page, wrapper);
        int total = (int) sysUserPage.getTotal();
        int pageSize = (int)page.getSize();
        MicroPage<SysUser> sysUserMicroPage = new MicroPage<>();
        sysUserMicroPage.setTotal(total); // 设置总记录数
        sysUserMicroPage.setPageData(sysUserPage.getRecords()); // 设置当前页的数据列表
        sysUserMicroPage.setPageNum(current); // 设置当前页码
        sysUserMicroPage.setPageSize(size); // 设置每页条数
        Integer pages = (int) Math.ceil((double) total / pageSize);
        sysUserMicroPage.setPages(pages); // 设置总页数
        return sysUserMicroPage;
    }

    @Loggable
    // 升降序分页查询接口
    public MicroPage<SysUser> page2(int current, int size, SysUserCondition condition, String sortField, String sortOrder) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();

        // 构建模糊查询条件
        if (condition != null && condition.getName() != null) {
            wrapper.like("name", condition.getName()).or();
        }
        if (condition != null && condition.getAge() != null) {
            wrapper.like("age", condition.getAge()).or();
        }
        if (condition != null && condition.getEmail() != null) {
            wrapper.like("email", condition.getEmail()).or();
        }

        // 设置排序规则
        if ("desc".equalsIgnoreCase(sortOrder)) {
            wrapper.orderByDesc(sortField);  // 降序排序
        } else {
            wrapper.orderByAsc(sortField);   // 升序排序（默认）
        }

        // 创建分页对象
        Page<SysUser> page = new Page<>(current, size);
        // 执行分页查询
        Page<SysUser> sysUserPage = sysUserMapper.selectPage(page, wrapper);

        // 计算总记录数、页数
        int total = (int) sysUserPage.getTotal();
        int pageSize = (int) page.getSize();

        // 封装返回的分页结果
        MicroPage<SysUser> sysUserMicroPage = new MicroPage<>();
        sysUserMicroPage.setTotal(total);
        sysUserMicroPage.setPageData(sysUserPage.getRecords());
        sysUserMicroPage.setPageNum(current);
        sysUserMicroPage.setPageSize(size);

        // 计算总页数
        Integer pages = (int) Math.ceil((double) total / pageSize);
        sysUserMicroPage.setPages(pages);

        return sysUserMicroPage;
    }

}
